<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
if($_RQ['op']=='getteams'){
	$goodsid=intval($_RQ['goodsid']);
	$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	$goods=$this->model('model')->up_price($goods,$return['pluginset'],true);
	$groupnum=$goods['teamnum']?:2;
	$pindex = max(1, intval($_RQ['page']));
	$psize =6;
	$return['params']=$params=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'status'=>0);
	$status=intval($_RQ['status']);
	$num=intval($_RQ['num']);
	if($_RQ['keyword']){
		$params['title like']='%'.$_RQ['keyword'].'%';
		$return['params']['keyword']=$_RQ['keyword'];
	}
	if($num){
		$psize = $num;
	}
	$times=array();
	$goods['grouptime']=$goods['grouptime']?:48;
	$list=pdo_getslice('jy_weishop_groups_team',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach ($list as $k=>&$team) {
			//查询对应拼团还需人数
			$num1 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_newping','teamid'=>$team['id'],'status'=>10));
			if(!$num1){
				$this->model('model')->up_team2($team['id']);
				unset($list[$k]);
			}
			$team['num'] = $groupnum - $num1;
			//查询团长信息
			$team['header']=cfc('core')->member_show($team['mid']);
			//查询团长地址
			$team['address']=model('core/member')->get_address_str($team['addressid']);
			$times[$team['id']]=model('jy_weishop/model')->date($team['createtime']+$goods['grouptime']*60-TIMESTAMP);
		}
	}
	$return['times']=$times;
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='getoneteamorder'){
	$teamid=intval($_RQ['teamid']);
	$goodsid=intval($_RQ['goodsid']);
	$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	$goods=$this->model('model')->up_price($goods,$return['pluginset'],true);
	$groupnum=$goods['teamnum']?:2;
	$num=0;
	if($teamid){
		//查询该团的所有人
		$teamcreatetime=pdo_getcolumn('jy_weishop_groups_team',array('id'=>$teamid,'uniacid'=>$_SESSION['uniacid']),'createtime');
		$orders=pdo_getall('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_newping','teamid'=>$teamid,'status'=>10));
		if($orders){
			foreach($orders as &$item){
				$item['member']=cfc('core')->member_show($item['mid']);
				$item['member']['avatar']=$item['member']['avatar']?tomedia($item['member']['avatar']):'';
				if($item['mid']==$team['mid']){
					$nickname=$item['member']['nickname'];
				}
				$item['paytime']=date('Y-m-d h:i',$item['paytime']);
			}
		}
		$num=count($orders);
	}
	$lastnum=$groupnum-$num;
	$lastorder=array();
	for ($i=1; $i<=$lastnum; $i++) {
		$lastorder[]=array('no'=>$i);
	}
	$return['teamsorder']=$orders?:array();
	$return['teamslastorder']=$lastorder?:array();
	//限时时间
	$hours = $goods['grouptime']?:48;
	$endtime = $hours*3600+$teamcreatetime;
	$lasttime2 = $endtime-time();
	$return['teamslasttime']=model('jy_weishop/model')->date($lasttime2);
	exi($return);
}
